/**
 * Created by zhanglihe on 15/10/30.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'template',
    'collection',
    'model',
    'dialog',
    'datepicker'
], function ($, common, ready, template, Collection, Model, dialog, datepicker) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            loading();
            this.fetch();
            this.event();
        },
        fetch: function (_args) {
            var _this = this;
            var args = _args || {};
            args.limit = 10;
            args.offset = 0;

            this.collection = new Collection({
                container: 'data-list',
                template: 'data-item',
                url: _global.url.api + 'agency/withdraw_list',
                condition: args,
                model: {
                    tag_name: 'tr',
                    event: {},
                    change: function (e) {
                        this.data[$(e.target).attr('name')] = $(e.target).val();
                    }
                },
                callback: function () {
                    loaded();
                }
            });
            this.collection.fetch();
        },
        event: function () {
            $('#start_time').on('click', function () {
                WdatePicker({
                    el: this.id,
                    dateFmt: 'yyyy-MM-dd HH:mm:ss'
                });
            });

            $('#end_time').on('click', function () {
                WdatePicker({
                    el: this.id,
                    dateFmt: 'yyyy-MM-dd HH:mm:ss'
                });
            });

            var _this = this;

            var reloadData = function() {
                var startTime = $('#start_time').val();
                var endTime = $('#end_time').val();
                var status = $('.btn-group .active').attr('data-item');

                _this.collection.condition.status = status;
                _this.collection.condition.start_time = startTime;
                _this.collection.condition.end_time = endTime;
                _this.collection.reload();
            }

            /**
             * 搜索事件
             */
            $('#btn-search').on('click', function (e) {
                e.preventDefault();

                reloadData();
            });

            $('.btn-group button').on('click', function(e) {
                $('.btn-group button').removeClass('active');
                $(this).addClass('active');

                var dataItem = $(this).attr('data-item');

                if (dataItem == 2) {
                    $('#withdraw').show();
                } else {
                    $('#withdraw').hide();
                }

                if (dataItem == 0) {
                    $('#all_button').show();
                } else {
                    $('#all_button').hide();
                }

                reloadData();

            });

            // 操作
            var withdrawAction = function(id, status) {
                var data = {};
                data.id = id;
                data.status = status;
                var url = _global.url.api + 'agency/withdraw_action';

                var requestContent = {
                    url: url,
                    data: data,
                    dataType: 'json',
                    type: 'post',
                    success: function(data) {
                        Alert({msg: data.msg, type: data.code == 0 ? 'success' : 'error'});
                        if (data.code == 0) {
                            reloadData();
                        }
                    }
                };
                if (status == 2) {
                    var d = dialog({
                        title: '拒绝原因',
                        content: '<textarea cols="50" rows="8" autofocus id="reject_content"></textarea>',
                        ok: function() {
                            c = $('#reject_content').val();
                            data.reject_content = c;
                            $.ajax(requestContent);
                        },
                        cancelValue: '取消',
                        cancel: true,
                    });
                    d.showModal();
                } else {
                    $.ajax(requestContent);
                }
            };

            $('body').on('click', '.agree', function() {
                withdrawAction($(this).attr('data-id'), 1);
            });

            $('body').on('click', '.reject', function() {
                withdrawAction($(this).attr('data-id'), 2);
            });

            $('#all').on('click', function() {
                $('td :checkbox').prop('checked', $(this).prop('checked'))
            });

            $('#all_agree').on('click', function() {
                var id = [];
                $('td :checkbox:checked').each(function(index, value) {
                    var v = $(value).val();
                    id.push(v);
                });
                if (id.length == 0) {
                    Alert({msg:"请至少选择一个有效的提现", type: "error"});
                    return false;
                }
                withdrawAction(id, 1);
            });

            $('#all_reject').on('click', function() {
                var id = [];
                $('td :checkbox:checked').each(function(index, value) {
                    var v = $(value).val();
                    id.push(v);
                });
                if (id.length == 0) {
                    Alert({msg:"请至少选择一个有效的提现", type: "error"});
                    return false;
                }
                withdrawAction(id, 2);
            });
        },

        change: function (e) {
            this.data[$(e.target).attr('name')] = $(e.target).val();
        }

    };

    new index();

});